﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using HtmlAgilityPack;
using Newtonsoft.Json;
using BaiDuMusic;

namespace HTMLSolution
{
    class Program
    {
        static void Main(string[] args)
        {

            //TestOne();

            //TestTwo();
            //TestThree();
            //TestFourth();
            TestFive();

            DateTime start = new DateTime(1970, 1, 1);
            int time = (int)(DateTime.Now - start).TotalSeconds;
            Console.WriteLine("时间戳：" + time);
            Console.Read();
        }
        public static void TestFive()
        {
            //BaiDuMusic music = new BaiDuMusic();
            //List<MusicSearchData> list = music.LoadPage("海阔天空");
            //Console.WriteLine(JsonConvert.SerializeObject(list));

            BaiDuSong song = new BaiDuSong("14795583");
            Console.WriteLine(JsonConvert.SerializeObject(song.CurrentSong));
        }

        public static void TestFourth()
        {
            //生成DOM字符串结构
            HtmlNode container = HtmlNode.CreateNode("<div />");
            HtmlNode title = HtmlNode.CreateNode("<h3 />");
            title.InnerHtml = "张三丰";
            HtmlNode link = HtmlNode.CreateNode("<a />");
            link.InnerHtml = "点击进入";
            link.SetAttributeValue("href", "http://wwww.gongjuji.net");
            container.AppendChild(title).AppendChild(link);
            Console.WriteLine(container.OuterHtml);
        }
        public static void TestThree()
        {
            //解析html 字符串或者本地html文件
            HtmlDocument doc = new HtmlDocument();
            string html = "<div id=\"demo\"><span style=\"color: red; \"><h1>Hello</h1> </span></div>";
            doc.LoadHtml(html);
            HtmlNode demo = doc.GetElementbyId("demo");

            Console.WriteLine(demo.InnerHtml);
            //注：InnerText中会有换行或空格等，需要特殊处理
            Console.WriteLine(demo.InnerText);
            Console.WriteLine(demo.InnerText.Length);
        }
        public static void TestTwo()
        {
            //加载Web 的页面并解析内容
            string rowPath = "/html/body/div[2]/div[2]";
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load("http://www.gongjuji.net");
            //以document 为基准
            HtmlNode row = doc.DocumentNode.SelectSingleNode(rowPath);
            //创建row为基准
            row = HtmlNode.CreateNode(row.OuterHtml);
            HtmlNodeCollection titles = row.SelectNodes("//h3");
            foreach (var item in titles)
            {
                Console.WriteLine(item.InnerText);
            }
            HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a");
            foreach (HtmlNode item in links)
            {
                Console.WriteLine(item.Attributes["href"].Value);
            }
            Console.WriteLine("------->解析结束");
        }
        public static void TestOne()
        {
            //加载Web 的页面并解析内容
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load("http://www.gongjuji.net");
            HtmlNode row = doc.DocumentNode.SelectSingleNode("//div[@class='row']");
            //以document为基准查询
            HtmlNodeCollection col = row.SelectNodes("//div[@class='thumbnail']");
            foreach (HtmlNode item in col)
            {
                //使用CreateNode（）来创建新的查询基准
                HtmlNode thumbnail = HtmlNode.CreateNode(item.OuterHtml);
                HtmlNode title = thumbnail.SelectSingleNode("//h3");
                Console.Write("标题：" + title.InnerText);
                HtmlNode a = thumbnail.SelectSingleNode("//a");
                Console.WriteLine("\t链接：" + a.Attributes["href"].Value);
            }
            Console.WriteLine("------解析结束");
        }
    }
}
